/*
 * Copyright (c) 2007
 *	Alberto Magni, Maggioni Marcello
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
package spamassassin;
import java.io.*; 
/**SpamAssassinInterface has the role to call the external program SpamAssassin. The text of
 * the file to be sent is passed to the program and analized. Is also possible to get some
 * parameters from the application concerning the mail.
 * @author Marcello Maggioni
 * @author Alberto Magni*/
public class SpamAssassinInterface 
{	
    private String temp=new String();
    private String [] lines;
//------------------------------------------------------------------------------
    /**Calls SpamAssassin and set the output in a teporaneal string.
     * @param array the output of the mail analizer converted in an array of bytes
     * @throws IOException if an error with the byte streams occours*/
    public void callSpamAssassin(byte[] array) throws IOException
    {
        String[] command= {"spamassassin"};
        final Process process = Runtime.getRuntime().exec(command);
        InputStream is = process.getInputStream();
        OutputStream os = process.getOutputStream();
        os.write(array);
        os.flush();
        os.close();
        byte[] buffer = new byte[1024];
        while (is.read(buffer) >= 0)
        {
            for(int index=0;index<buffer.length;index++)
                temp=temp+((char)buffer[index]);
        }
    }
//------------------------------------------------------------------------------
    /**Get the spam rating of the checked mail
     * @return an array which have in first position the spam rate of the analized message
     * and in second position the limit over which a message is consider spam.
     *@throws Exception if an error reading the stream of bytes occours*/
    public Float [] getSpamRating() throws Exception
    {
    	boolean exit=false;
	//spam rating
	Float parameters [] = new Float [2];
	lines=temp.split("\n");
	for(int index =0;index<lines.length&&!exit;index++)
	{
		if(lines[index].startsWith("X-Spam-Status: "))
		{
			String words [] = lines[index].split(" ");
			String score=words[2];
			String limit=words[3];
			score=score.split("=")[1];
			limit=limit.split("=")[1];
			parameters[0]=Float.parseFloat(score);
			parameters[1]=Float.parseFloat(limit);
			exit=true;
		}
	}
	//the line of rating has not been found
	if(!exit)
	{
            throw new Exception();
	}
	else
	{
            return parameters;
	}
    }
//------------------------------------------------------------------------------
    /**Returns the list of the critical elements of the message to be sent.
     * @return a String rappresentig a table containig the warning in the message.
     It returns null if there is no table in the file.*/
    public String getCriticalElementsTable()
    {
	boolean exit=false;
	String table=new String("");
	for(int index =0;index<lines.length&&!exit;index++)
	{
            if(lines[index].startsWith(" pts rule name              description"))
            {
		exit=true;
		table=table+"<br>"+lines[index];
		for(int index2=index+1;index2<lines.length&&!(lines[index2].trim()).equals("");index2++)
		{
               		table=table+"<br>"+lines[index2];
		}
            }
	}
	if(table.equals(""))
            return null;
	else
            return table;
    }
}


